// pages/user/password/password.js
var app = getApp();
var countdown = 120;
var reget = '获取验证码';
var timer;
var isTimer = false;//timer是否开始
// 定时器
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_show: true,
    })
    countdown = 120;
    reget = '重新获取';
    isTimer = false;
    return;
  } else {
    that.setData({
      is_show: false,
      last_time: countdown
    })
    isTimer = true;
    countdown--;
    reget = '重新获取';
  }
  timer = setTimeout(function () {
    settime(that)
  }, 1000)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,//跳转的类型,2修改密码 1忘记密码

    tel: '',//手机号码
    code: '',//验证码
    original_password: '',//原密码
    newPassword: '',//新密码
    confimPassword: '',//确认密码
    getOnecode: reget,//按钮内容
    last_time: '',//剩余时间
    is_show: true,//是否显示倒计时
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      type: options.type||1
    })
    if (options.type==1){
      wx.setNavigationBarTitle({
        title: '忘记密码',
      })
    }else{
      wx.setNavigationBarTitle({
        title: '修改密码',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  //输入框输入
  inputChange(e) {
    var that = this;
    var name = e.currentTarget.dataset.name;
    var val = e.detail.value;
    that.setData({
      [name]: val
    })
  },

  //获取验证码
  getCode() {
    var that = this;
    console.log('isTimer:' + isTimer);
    if (isTimer) {
      console.log('timer已经开始');
      return;
    }
    var tel = that.data.tel;
    var numreg = /^[1][3-9][\d]{9}$/;
    if (tel == '') {
      app.showToast("手机号码不能为空", "none", 2000, function () { });
      return false;
    }
    if (numreg.test(tel) == false) {
      app.showToast("请输入正确手机的号码", "none", 2000, function () { });
      return false;
    }
    isTimer = true;
    app.ajax({
      url: "Common/Common/sendSms",
      data: { tel: tel },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            is_show: (!that.data.is_show)  //false
          })
          countdown = 120;
          reget = '获取验证码';
          settime(that); //刷新倒计时 
          that.setData({
            getOnecode: reget
          })
          app.showToast(res.data.message, "none", 4000, function () { });
        } else {
          app.showToast(res.data.message, "none", 2000, function () { });
        }
      }
    })
  },

  //提交数据
  submit() {
    var that = this;
    var tel = that.data.tel;
    var code = that.data.code;
    var original_password = that.data.original_password;//原密码
    var newPassword = that.data.newPassword;//新密码
    var confimPassword = that.data.confimPassword;//确认密码
    if (that.data.type == 2){
      if (original_password==''){
        app.showToast("原密码不能为空", "none", 2000, function () { });
        return false;
      }
    }
    if (that.data.type ==1 ){
      if (tel == '') {
        app.showToast("手机号码不能为空", "none", 2000, function () { });
        return false;
      } else if (code == '') {
        app.showToast("验证码不能为空", "none", 2000, function () { });
        return false;
      }
    }
    if (newPassword == '') {
      app.showToast("请输入新密码", "none", 2000, function () { });
      return false;
    } else if (confimPassword == '') {
      app.showToast("请再次确认密码", "none", 2000, function () { });
      return false;
    } else if (newPassword !== confimPassword){
      app.showToast("两次密码不一致", "none", 2000, function () { });
      return false;
    }
    var _data = {};
    if(that.data.type==1){
      _data = {
        type: that.data.type,
        tel: tel,
        code: code,
        newPassword: newPassword,
        confimPassword: confimPassword,
      };
    }else{
      _data = {
        type: that.data.type,
        user_id: wx.getStorageSync('user_id'),
        original_password: original_password,
        newPassword: newPassword,
        confimPassword: confimPassword,
      };
    }
    app.ajax({
      url: "User/User/changePassword",
      data: _data,
      success: function (res) {
        if (res.data.code == 1000) {
          app.showToast('修改成功');
          setTimeout(()=>{
            wx.navigateBack();
          },1000)
        }
      }
    })
  },
})